package springtest.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import springtest.dao.ImportSolrDao;
import springtest.nettySocketio.listener.SendDateListener;
import springtest.nettySocketio.service.SendDataService;
import springtest.pojo.Oplog;
import springtest.service.OplogService;
import springtest.utils.JsonUtils;

@Controller
public class MongoToSolrController {

    @Autowired
    private ImportSolrDao dao;
    // @Autowired
    // private ReadOplog readOplog;
    @Autowired
    private OplogService oplogService;

    // @RequestMapping("/*")
    // public void hah() {
    // System.out.println(123);
    // }
    @RequestMapping("/testCas")
    @ResponseBody
    public String testCas(HttpServletRequest request) {
        AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
        if (principal != null) {
            // Collections.unmodifiableMap(m);
            String username = principal.getName();
            Map<String, Object> attributes = principal.getAttributes();
            System.out.println("11" + attributes.get("salt"));
            String name1 = (String) attributes.get("name");
            String phone = (String) attributes.get("phone");
            // Cookie[] cookies = request.getCookies();
            // for (Cookie cookie : cookies) {
            // if (cookie.getName().equals("JSESSIONID")) {
            // System.out.println("----------");
            // System.out.println(cookie.getValue().toString());
            // Object object =
            // request.getSession().getAttribute(cookie.getValue().toString());
            // // if (object != null) {
            // System.out.println(object);
            // // }
            // }
            // }
            System.out.println(username);
            System.out.println(name1);
            System.out.println(phone);
            System.out.println(attributes.toString());
            System.out.println("--------------------");
        }
        return "11";
    }

    @RequestMapping("/testLogout")
    public String testLogout(HttpSession session) {
        session.invalidate();
        // 退出登录后，跳转到退成成功的页面，不走默认页面
        return "redirect:http://localhost:8080/cas/logout";
    }

    @RequestMapping("/oplog")
    @ResponseBody
    public String hahha(Oplog oplog) {
        System.out.println(oplog.toString());
        oplogService.slaveMongo(oplog);
        return "123";
    }

    @RequestMapping("/readSolr")
    @ResponseBody
    public String hehie() {
        List<Oplog> list = oplogService.getData();
        String result = JsonUtils.objectToJson(list);
        return result;
    }

    @Autowired
    private SendDateListener sendNumWarnListener;
    @Autowired
    private SendDataService sendDataService;

    // 数据从mongo导入solr
    @RequestMapping("/solr7/save")
    @ResponseBody
    public String MongoToSolrSave(String name, HttpServletRequest request) throws Exception {
        // mongoToSolrService.addIndex();
        // mon.testAllAdd();
        // mon.addIndex();
        // dao.importSolr();
        // dao.testMonNumber();
        // System.out.println(name);
        // oplogService.testTailableCursor();
        // String salt = PasswordUtils.secureRandomBytesInHex(8);
        // String password = PasswordUtils.encodePassword("123456",
        // "fe032dae83766435");
        // System.out.println("加密密钥：" + "fe032dae83766435" + "加密后的密码：" +
        // password);
        // testCas(request);
        // System.out.println(request.getRequestURL());
        // System.out.println(request.getHeader("referer"));
        sendDataService.sendData();
        return "1";
    }

    @RequestMapping("/cas/test22")
    public ModelAndView aa() {
        System.out.println(123);

        return new ModelAndView("view");
    }
}
